package com.zy.cat.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.zy.cat.common.annotation.RedisEvicts.RedisEvict;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatAdService;
@Service
public class CatAdServiceImpl implements CatAdService {
	
	@Autowired
	@Qualifier("redis14")
	RedisTemplate<String, String> redis14;
	
	@Autowired
	CacheService cacheService;
	
	/* 全端广告统计 */
	private final String AD_ALL_KEY="AD_CAT_APP_ALL";
	/* 安卓端广告统计 */
	private final String AD_AN_KEY="AD_CAT_APP_AN";
	/* IOS端广告统计 */
	private final String AD_IOS_KEY="AD_CAT_APP_IOS";

	@Override
	@Async("zyasyncPoolTaskExecutor")
	public void insertAdOper(String pid, int rtype, int oper, int ftype,Integer ltype, String brand, String model, String vscode,
			String cid, String imei, String imsi) {
		String ntime=LocalDate.now().toString();
		Map<String, List<String>> map=new HashMap<String, List<String>>();
		//key格式：广告平台_是否翻倍_广告类型(展示/点击)_(PID) 例：TX_FB_SHOW_1225252或TX_FB_SHOW
		StringBuilder builderKey=new StringBuilder(ltype==0?"CSJ":"TX");
		builderKey.append("_");
		builderKey.append(ftype==1?"FB":"FFB");
		builderKey.append("_");
		String akey=builderKey.toString(); 
//		/* 翻倍广告展示统计 */
//		String FB_AD_SHOW="FBSHOW_"+cid+"_PID_"+pid;
//		
//		/* 翻倍广告点击统计 */
//		String FB_AD_CLICK="FBCLICK_"+cid+"_PID_"+pid;
//		
// 
//		/* 非翻倍广告展示统计 */
//		String FFB_AD_SHOW="FFBSHOW_"+cid+"_PID_"+pid;
//		
//		/* 非翻倍广告点击统计 */
//		String FFB_AD_CLICK="FFBCLICK_"+cid+"_PID_"+pid;
		if(oper==0) {
			builderKey.append("SHOW_"+pid);
			akey+="SHOW_";
		}else {
			builderKey.append("CLICK_"+pid);
			akey+="CLICK_";
		}
		
		builderKey.append("_"+vscode);
		akey+="_"+vscode;
		
		String[]  items= {builderKey.toString(),akey};
		 
		List<String> list=Arrays.asList(items);
		if(rtype==0) {
			map.put(AD_AN_KEY+"_"+ntime, list);
		}else if(rtype==1) {
			map.put(AD_IOS_KEY+"_"+ntime, list);
		}
		map.put(AD_ALL_KEY+"_"+ntime, list);
		if(!map.isEmpty()) {
			redis14.executePipelined(new RedisCallback<Long>() {
	            public Long doInRedis(RedisConnection connection) throws DataAccessException {
	                connection.openPipeline();
	                map.forEach((key,hkeys)->{
	                	hkeys.forEach(hkey->{
		                	connection.hIncrBy(key.getBytes(), hkey.getBytes(), 1);	
	                	});
	                	connection.expire(key.getBytes(), 60*60*24*2);
	                });
	                return null;
	            }
	        });
		}
	}

	@Override
	public Integer getAdSwitch(Integer rtype, String brand, String model, String vscode,
			String cid, String imei, String imsi) {
		//获取广告开关按钮设置
		return cacheService.getCatAppAdStatus(rtype,cid,vscode);
	}

}
